package org.example.binding;

import org.example.session.SqlSession;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;

public class MapperProxy<T> implements InvocationHandler, Serializable {
    private final SqlSession sqlSession;
    private final Class<T> mapperInterface;

    public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface) {
        this.sqlSession = sqlSession;
        this.mapperInterface = mapperInterface;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            System.out.println("mapper调用前...");

            Object result = new MapperMethod(mapperInterface, method, sqlSession).execute(args);
            System.out.println("mapper调用后...");
            return result;
        } catch (Throwable t) {
            t.printStackTrace();
            throw new RuntimeException("Error invoking method " + method);
        }
    }
}
